package p.ithorns.example.statemachine.test;

import lombok.extern.slf4j.Slf4j;
import p.ithorns.example.statemachine.core.Action;
import p.ithorns.example.statemachine.core.Event;
import p.ithorns.example.statemachine.core.EventEnum;
import p.ithorns.example.statemachine.core.State;

/**
 * FlowAction
 * 状态转换抽象类
 *
 * @author ithorns
 * @version 1.0.0
 * @date 2022/11/13 13:16
 */
@Slf4j
public abstract class FlowAction implements Action {

    private EventEnum eventType;

    private State curState;

    private State nextState;


    public FlowAction() {
    }

    public FlowAction(State curState, State nextState) {
        this.curState = curState;
        this.nextState = nextState;
    }

    public FlowAction(EventEnum eventType, State curState, State nextState) {
        this.eventType = eventType;
        this.curState = curState;
        this.nextState = nextState;
    }

    @Override
    public State execute(Event event) {
        if (doExecute(event)) {
            return nextState;
        }
        return curState;
    }

    @Override
    public EventEnum getEventType() {
        return eventType;
    }

    /**
     * 执行动作的具体业务
     *
     * @param event -事件
     * @return boolean
     */
    public abstract boolean doExecute(Event event);

}